package com.demo.accessibilitykeeper;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;

/**
 * 保活服务 - 9月21号稳定版本
 * 通过多种策略保持应用活跃
 */
public class KeepAliveService extends Service {

    private static final String TAG = "KeepAliveService";
    private boolean isRunning = false;
    private Handler mainHandler;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "KeepAliveService onCreate");
        
        mainHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "KeepAliveService onStartCommand");
        
        if (!isRunning) {
            startKeepAliveStrategies();
            isRunning = true;
        }
        
        // 返回START_STICKY确保服务被杀死后自动重启
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "KeepAliveService onDestroy");
        isRunning = false;
    }

    /**
     * 启动保活策略
     */
    private void startKeepAliveStrategies() {
        try {
            Log.d(TAG, "🛡️ STARTING_KEEPALIVE_STRATEGIES");
            
            // 策略1: 定期重启服务
            startPeriodicRestart();
            
            // 策略2: 启动前台服务
            startForegroundService();
            
            // 策略3: 启动无障碍服务
            startAccessibilityService();
            
            Log.d(TAG, "✅ KEEPALIVE_STRATEGIES_STARTED");
            
        } catch (Exception e) {
            Log.e(TAG, "❌ ERROR_STARTING_KEEPALIVE_STRATEGIES", e);
        }
    }

    /**
     * 定期重启服务
     */
    private void startPeriodicRestart() {
        try {
            Log.d(TAG, "🔄 STARTING_PERIODIC_RESTART");
            
            Runnable restartRunnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        // 重启自己
                        Intent restartIntent = new Intent(KeepAliveService.this, KeepAliveService.class);
                        startService(restartIntent);
                        
                        Log.d(TAG, "🔄 PERIODIC_RESTART_EXECUTED");
                        
                    } catch (Exception e) {
                        Log.e(TAG, "❌ ERROR_IN_PERIODIC_RESTART", e);
                    }
                    
                    // 每5分钟重启一次
                    mainHandler.postDelayed(this, 300000);
                }
            };
            
            // 延迟1分钟后开始定期重启
            mainHandler.postDelayed(restartRunnable, 60000);
            
            Log.d(TAG, "✅ PERIODIC_RESTART_STARTED");
            
        } catch (Exception e) {
            Log.e(TAG, "❌ ERROR_STARTING_PERIODIC_RESTART", e);
        }
    }

    /**
     * 启动前台服务
     */
    private void startForegroundService() {
        try {
            Log.d(TAG, "📱 STARTING_FOREGROUND_SERVICE");
            
            Intent foregroundIntent = new Intent(this, ForegroundService.class);
            startService(foregroundIntent);
            
            Log.d(TAG, "✅ FOREGROUND_SERVICE_STARTED");
            
        } catch (Exception e) {
            Log.e(TAG, "❌ ERROR_STARTING_FOREGROUND_SERVICE", e);
        }
    }

    /**
     * 启动无障碍服务
     */
    private void startAccessibilityService() {
        try {
            Log.d(TAG, "♿ STARTING_ACCESSIBILITY_SERVICE");
            
            Intent serviceIntent = new Intent(this, AccessibilityKeeperService.class);
            startService(serviceIntent);
            
            Log.d(TAG, "✅ ACCESSIBILITY_SERVICE_STARTED");
            
        } catch (Exception e) {
            Log.e(TAG, "❌ ERROR_STARTING_ACCESSIBILITY_SERVICE", e);
        }
    }
}